<template>
	<view class="page">
		<view class="mine">
			<view class="cardInfo-wrapper" @click="toUrl('/pages/myMessage/myMessage')">
				<!--toUrl('/pages/myMessage/myMessage')-->
				<view class="cardInfo">
					<view class="information">
						<view class="avatar-wrapper"><img :src="photo" mode="aspectFill" alt="无头像喵" class="avatar" />
						</view>
						<!-- <view class="avatar"></view> -->
						<view class="info-docker">
							<view class="titles">
								<text class="title">姓名</text>
								<text class="title">性别</text>
								<text class="title">年龄</text>
								<text class="title">所在地</text>
							</view>
							<view class="contents">
								<text class="content">{{userData.childrenName}}</text>
								<text class="content">{{userData.sex}}</text>
								<text class="content">{{userData.age}}</text>
								<text class="content">{{userData.location}}</text>
							</view>
						</view>
					</view>
					<view class="tags-docker">
						<text class="signature">{{userData.signature}}</text>
					</view>
				</view>
			</view>
			<mystory class="mystory" :userData="userData"></mystory>
			<mycontribute class="mycontribute" :userData="userData" :donate="donate"></mycontribute>
			<recent class="recent" :recentData="recentData" :days="days"></recent>
			<!-- <testcomponent></testcomponent> -->
		</view>
	</view>
</template>

<script>
	import {
		BASE_URL
	} from '../../api';
	import {
		mapState,
		mapActions
	} from 'vuex';
	export default {
		computed: {
			...mapState(['token', 'children'])
		},
		data() {
			return {
				msgType: 'success',
				value: '默认输入的内容',
				id: 'qweasdzxc',
				text: '',
				userData: {},
				photo: '../../static/头像.jpg',
				donate: {},
				recentData: [],
				days: [],
			}
		},
		onShow() {
			console.log(this.token);
			console.log(this.children);
			this.id = this.children.childrenId;
			this.testGetUser();
		},
		created() {
		},
		methods: {
			toUrl(url) {
				url = url + '?data=' + JSON.stringify(this.userData) + '&photo=' + JSON.stringify(this.photo)
				uni.navigateTo({
					url
				});
			},
			recent() {
				uni.navigateTo({
					url: '/pages/recent/recent'
				});
			},
			formatDate(date, format) {
			    const map = {
			        mm: date.getMonth() + 1,
			        dd: date.getDate(),
			        yy: date.getFullYear().toString().slice(-2),
			        yyyy: date.getFullYear()
			    }
			
			    return format.replace(/mm|dd|yy|yyy/gi, matched => map[matched])
			},
			testGetUser() {
				let curTime = new Date().getTime();
				let startDate = curTime - (10 * 3600 * 24 * 1000);
				startDate = new Date(startDate).toISOString();
				startDate = startDate.slice(0,10);
				let endDate = new Date(curTime).toISOString();
				endDate = endDate.slice(0,10);
				console.log("日期：");
				console.log(startDate);
				
				uni.request({
					url: BASE_URL + '/children/' + this.id,
					method: 'GET',
					header: {
						"Authorization": this.token,
					},
					success: (res) => {
						this.userData = res.data.data;
						console.log(res.data);
						console.log(this.userData);
						this.text = 'request success';
						console.log(this.text);
					}
				});
				uni.request({
					url: BASE_URL + '/photo/selectOneByChildrenId',
					method: 'GET',
					header: {
						"Authorization": this.token,
					},
					data: {
						children_id: this.id,
					},
					success: (res) => {
						console.log(res.data);
						this.photo = res.data.data;
						console.log(this.photo);
						this.text = 'request success';
						console.log(this.text);
					}
				});
				console.log('启动！');
				uni.request({
					url: BASE_URL + '/donorChildrenRelation/getDonateRecordByChildrenId',
					method: 'GET',
					header: {
						"Authorization": this.token,
					},
					data: {
						childrenId: this.id,
					},
					success: (res) => {
						this.donate = res.data.data;
						console.log(this.donate);
						this.text = 'request success';
						console.log(this.text);

						let len = this.donate.length;
						let value = 0;
						for (let i = 0; i < len; i++) {
							console.log(this.donate[i].donateMoney);
							value += this.donate[i].donateMoney;
						}
						Object.assign(this.donate, {
							total: value
						});
						console.log(this.donate)
					}
				});

				uni.request({
					url: BASE_URL + '/taskEvaluateRecord/getTaskEvaluateRecordByChildrenIdWithTime?childrenId='
					+this.id+'&startTimes='+startDate+'&endTimes='+endDate,
					method: 'GET',
					header: {
						"Authorization": this.token,
					},
					data: {
						// childrenId: this.id,
						// startTime: startDate,
						// endTime: endDate,
					},
					success: (res) => {
						this.recentData = res.data.data;
						console.log(this.recentData);
						this.text = 'request success';
						console.log(this.text);

						this.days = Object.keys(this.recentData).sort().reverse();
						console.log(this.days);
					}
				});
				// uni.$emit('getUser',this.userData)
			},
			...mapActions(['askToken', 'updateChildren',
			])
		}
	}
</script>

<style>
	@import url("my.css");
</style>